package com.sxc.study;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.sxc.loglib.FileUtils;
import com.sxc.loglib.ui.LogActivity;
import com.sxc.study.ndk.JNIUtils;
import com.sxc.study.ndk.JniTest;
import com.sxc.study.tts.TTSActivity;

import java.io.File;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private RecyclerView rv;
    private LogAdapter adapter;
    private String rootPath = Environment.getExternalStorageDirectory().getPath() + "/1.apk";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: start");


        String jniUtils = JNIUtils.printStringByJni();
        Log.d(TAG, "onCreate: " + JniTest.getJniString());
//        rootPath = getFilesDir().getAbsolutePath() + "/1.apk";
        findViewById(R.id.recycler_view_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Log.d(TAG, "onClick: v");
                startActivity(new Intent(MainActivity.this, PoolActivity.class));
            }
        });

        findViewById(R.id.log_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File[] files = FileUtils.getLogFileList();
                if (files != null) {
                    adapter.add(Arrays.asList(files));
                }
            }
        });

        findViewById(R.id.alarm_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, AlarmManagerActivity.class));
            }
        });

        findViewById(R.id.crash).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                throw new NullPointerException("test crash");
            }
        });

        findViewById(R.id.silent_install).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Uri uri = Uri.fromFile(new File(rootPath));
//                CustomPackageManager mCustomPM = new CustomPackageManager(MainActivity.this);
//                mCustomPM.installPackage(uri, new OnAppInstallObserver() {
//                    @Override
//                    public void packageInstalled(String s, int i) throws RemoteException {
//                        Log.d(TAG, "packageInstalled: " + s + "====" + i);
//                    }
//                }, CustomPackageManager.INSTALL_REPLACE_EXISTING, MainActivity.this.getPackageName());
                if (!SilentInstallUtil.isRoot()) {
                    Log.d(TAG, "onClick: 没有ROOT权限，不能使用秒装");
                    return;
                }
                File file = new File(rootPath);
                if (TextUtils.isEmpty(rootPath) && file.exists()) {
                    Log.d(TAG, "onClick: 请选择安装包！");
                    return;
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        boolean isSuccess = SilentInstallUtil.RootSilentInstall("com.sxc.study", rootPath);
                        Log.d(TAG, "run: isSuccess=======>" + isSuccess);
//                        SilentInstallUtil.install(new File(rootPath));
                    }
                }).start();
            }
        });

        findViewById(R.id.tts).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, TTSActivity.class));
            }
        });

        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        rv.setAdapter(adapter = new LogAdapter());
        rv.setItemAnimator(new DefaultItemAnimator());

        adapter.setOnItemClickListener(new LogAdapter.OnItemClickListener() {
            @Override
            public void onItemClicked(final File file) {
//                AnyLayer.dialog(MainActivity.this)
//                        .contentView(R.layout.dialog_fullscreen)
//                        .backgroundColorRes(android.R.color.white)
//                        .onClickToDismiss(R.id.close)
//                        .onShowListener(new Layer.OnShowListener() {
//                            @Override
//                            public void onShowing(Layer layer) {
//
//                            }
//
//                            @Override
//                            public void onShown(Layer layer) {
//                                TextView tv = layer.getView(R.id.iv_1);
//                                if (file != null) {
//                                    tv.setText(FileUtils.readLogText(file));
//                                }
//                            }
//                        })
//                        .show();
            }
        });

        findViewById(R.id.log).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogActivity.startLogActivity(MainActivity.this);
            }
        });


        findViewById(R.id.ppt_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent = new Intent();
                    intent.setAction("com.dahuatech.camera.Action_stop_VideoRecord");
                    intent.putExtra("cameraid", 0);//启动相机0
                    intent.setPackage("com.dahuatech.camera");
                    startService(intent);
                }catch (Exception e){
                    Log.e(TAG, "onClick: ", e);
                }
            }
        });

        String[] pwds = new String[]{"nari01", "nari02", "nari03", "nari04", "nari05"};

        for (String pwd : pwds) {
            String encryptionPwd = MD5Util.MD5(pwd);
            Log.d(TAG, "onCreate: " + pwd + "=====" + encryptionPwd);
        }
    }
}
